package com.le.book.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MainWin {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainWin window = new MainWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(MainWin.class, "/images/yclogo.png"));
		shell.setSize(1014, 720);
		shell.setText("图书管理系统");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("基础数据");
		
		Menu menu_1 = new Menu(menuItem);
		menuItem.setMenu(menu_1);
		
		MenuItem menuItem_1 = new MenuItem(menu_1, SWT.NONE);
		menuItem_1.setText("分类管理");
		
		MenuItem menuItem_2 = new MenuItem(menu_1, SWT.NONE);
		menuItem_2.setText("图书管理");
		
		MenuItem menuItem_3 = new MenuItem(menu_1, SWT.NONE);
		menuItem_3.setText("用户查询");
		
		MenuItem menuItem_4 = new MenuItem(menu, SWT.CASCADE);
		menuItem_4.setText("报表统计");
		
		Menu menu_2 = new Menu(menuItem_4);
		menuItem_4.setMenu(menu_2);
		
		MenuItem menuItem_5 = new MenuItem(menu_2, SWT.NONE);
		menuItem_5.setText("阅读量统计");
		
		MenuItem menuItem_6 = new MenuItem(menu_2, SWT.NONE);
		menuItem_6.setText("活跃度统计");
		
		MenuItem menuItem_7 = new MenuItem(menu_2, SWT.NONE);
		menuItem_7.setText("逾期统计");
		
		MenuItem menuItem_8 = new MenuItem(menu, SWT.CASCADE);
		menuItem_8.setText("帮助");
		
		Menu menu_3 = new Menu(menuItem_8);
		menuItem_8.setMenu(menu_3);
		
		MenuItem menuItem_9 = new MenuItem(menu_3, SWT.NONE);
		menuItem_9.setText("使用帮助");
		
		MenuItem menuItem_10 = new MenuItem(menu_3, SWT.NONE);
		menuItem_10.setText("关于我们");
		
		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		
		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("首页");
		
		Label lblNewLabel = new Label(tabFolder, SWT.NONE);
		lblNewLabel.setImage(SWTResourceManager.getImage(MainWin.class, "/images/ShouYe.jpg"));
		tabItem.setControl(lblNewLabel);
		
		// 图书管理
		menuItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// 获取所有标签页
				TabItem[] items = tabFolder.getItems();
				for (int i=0; i<items.length; i++) {
					if ("图书管理".equals(items[i].getText())) {
						// 如果已经打开，则显示该页
						tabFolder.setSelection(i);
						return;
					}
				}
				
				TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
				tabItem.setText("图书管理");
				BookMgrCmp bmc = new BookMgrCmp(tabFolder, SWT.NONE);
				tabItem.setControl(bmc);
				// 显示该页
				tabFolder.setSelection(tabItem);
			}
		});

	}
}
